package com.openresty.bbs.controller;

import com.openresty.dao.common.utils.ResponseResult;
import com.openresty.dao.service.IRedisService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.openresty.dao.entity.User;


@RestController
public class IndexController {
    // @Autowired(required = false) // 修改默认值，允许为空

    @Autowired
    private IRedisService<User> redisService;

    //@ApiOperation("hello")
    @GetMapping("/hello")
    public String sayHello(@RequestParam(required = false, name = "who2") String who) {

        return who;
    }

    //@ApiOperation("hello2")
    @GetMapping("/hello2")
    public ResponseEntity<String> hello() {
        return new ResponseEntity<>("hello world", HttpStatus.OK);
    }



    /**
     * @param user user param
     * @return user
     */
    @ApiOperation("Add")
    @PostMapping("/add")
    public ResponseResult<User> add(User user) {
        redisService.set(String.valueOf(user.getId()), user);
        return ResponseResult.success(redisService.get(String.valueOf(user.getId())));
    }

    /**
     * @return user list
     */
    @ApiOperation("Find")
    @GetMapping("/find/{userId}")
    public ResponseResult<User> edit(@PathVariable("userId") String userId) {
        return ResponseResult.success(redisService.get(userId));
    }
}
